<?php
include_once 'inc/init.php';
$error = "";
$msg = "";
$fileElementName = 'fileToUpload';

if (empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none') {
    $error = 'No file was uploaded..';
} else {
    //$msg .= " File Name: " . $_FILES['fileToUpload']['name'] . ", ";
    //$msg .= " File Size: " . @filesize($_FILES['fileToUpload']['tmp_name']);

    $uploadfolder = $imagepath ;//'uploaded/';
    $source = $_FILES['fileToUpload']['tmp_name'];
    //$dest = $uploadfolder . $_FILES['fileToUpload']['name'];
    $ext = pathinfo($_FILES['fileToUpload']['name'], PATHINFO_EXTENSION);
    $dest = $uploadfolder . uniqid() . '.' . $ext;
    
    $i = 0;
    while (file_exists($dest)) {
        $dest = $uploadfolder . $i . $_FILES['fileToUpload']['name'];
        $i++;
    }
    
    copy($source, $dest);
    $msg = $dest;
}
echo "{";
echo "error: '" . $error . "',\n";
echo "msg: '" . $msg . "'\n";
echo "}";
?>